﻿using GDGeek;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace MrPP.UX { 
    public abstract class UIEffect : MonoBehaviour
    {
        [SerializeField]
        private UnityEvent _onShow;
        [SerializeField]
        private UnityEvent _onHide;

        public abstract void init();
        public GDGeek.Task show() {
            GDGeek.Task task = showImpl();
            TaskManager.PushBack(task, delegate {
                _onShow?.Invoke();
            });
            return task;
        }
        public GDGeek.Task hide() {


            GDGeek.Task task = hideImpl();
            TaskManager.PushFront(task, delegate {
                _onHide?.Invoke();
            });
            return task;
        }


        protected abstract GDGeek.Task showImpl();
        protected abstract GDGeek.Task hideImpl();
        private int layer_ = -1;
        public int layer {
            get {
                if (layer_ == -1) {
                    layer_ = 0;
                    Transform trans = this.transform;
                    while (trans != null && trans.GetComponent<UIEffectRunner>() == null) {
                        if (trans.GetComponent<UIEffect>() != null) {
                            layer_++;
                        }
                        trans = trans.parent;// trans.transform = 
                        
                    }


                }
                return layer_;
            }
        }
    }
}